/**
 *      example of use :
 *              add draggable="true" attr to element
 *              app.dragndrop('#ololo',{
 *                      ondragstart: function(e){...},
 *                      ondrag: function(e){...},
 *                      ondragcenter: function(e){...},
 *                      ondragleave: function(e){...},
 *                      ondragover: function(e){...},
 *                      ondrop: function(e){...},
 *                      ondragend: function(e){...}
 *              });
 */
(function () {
  var namespace = namespace || 'app';
  if(typeof (window[namespace]) === 'undefined') {
    return;
  }
  var _dragstart;
  var _drag;
  var _dragcenter;
  var _dragleave;
  var _dragover;
  var _drop;
  var _dragend;
  var init = function (_) {
      _dragstart = _.ondragstart ||
      function () {};
      _drag = _.ondrag ||
      function () {};
      _dragcenter = _.ondragcenter ||
      function () {};
      _dragleave = _.ondragleave ||
      function () {};
      _dragover = _.ondragover ||
      function () {};
      _drop = _.ondrop ||
      function () {};
      _dragend = _.ondragend ||
      function () {};
    }
  var construct = function (selector, options) {
      var ar = ['ondragstart', 'ondrag', 'ondragcenter', 'ondragleave', 'ondragover', 'ondrop', 'ondragend'];
      options = options || {};
      for(var i = 0; i < ar.length; i++) {
        if(!options.hasOwnProperty(ar[i])) {
          options[ar[i]] = '';
        }
      }
      init(options);
      var elements = document.querySelector(selector);
      for(var el in elements) {
        el.addEventListener('dragstart', _dragstart, false);
        el.addEventListener('drag', _drag, false);
        el.addEventListener('dragcenter', _dragcenter, false);
        el.addEventListener('dragleave', _dragleave, false);
        el.addEventListener('dragover', _dragover, false);
        el.addEventListener('drop', _drop, false);
        el.addEventListener('dragend', _dragend, false);
      }
    };
  window[namespace].dragndrop = construct;
}());
